
import { useMount } from 'ahooks'
import { useRequest } from "alova/client"
import { useState, lazy, Suspense } from 'react'



import { Http } from '../Utils/Api'
import { ModelBox } from '../Model'
import { AddIndexdDB, IndexedDBClear } from '../Utils/IndexedDB'




const { hostname } = window.location

const LoadSvg = lazy(() => import('../View/Loading/LoadSvg'))

const index = () => {
    const [Template, setTemplate] = useState()
    const { send } = useRequest(params => Http({ url: 'getProjectConfigInfo', params }), { immediate: false })

    useMount(() => {
        if (hostname === 'localhost') {
            const temp = lazy(() => import(`./page/${hostname}.jsx`))
            setTemplate(temp)
        } else {
            IndexedDBClear()
            send({ WebUrl: hostname }).then(async res => {
                const { key, config } = res
                const [project] = config
                const { client_Id, authUrl, apiUrl, imageUrl } = project
                window.AUTH = authUrl
                window.API = apiUrl
                window.IMG = imageUrl
                await AddIndexdDB('key', key)
                await AddIndexdDB('project', project)
                const temp = lazy(() => import(`./page/${client_Id}.jsx`))
                setTemplate(temp)
            })
        }
    })

    return (
        <Suspense>
            {Template ? <Template /> : <LoadSvg />}
            <ModelBox />
        </Suspense>
    );
};

export default index;